package cn.tedu._03vehicle.controller;

import cn.tedu._03vehicle.base.response.JsonResult;
import cn.tedu._03vehicle.base.response.StatusCode;
import cn.tedu._03vehicle.mapper.UserMapper;
import cn.tedu._03vehicle.pojo.dto.UserLoginParam;
import cn.tedu._03vehicle.pojo.dto.UserRegParam;
import cn.tedu._03vehicle.pojo.entity.User;
import cn.tedu._03vehicle.pojo.vo.UserVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Slf4j
@RestController
@Tag(name = "01-用户模块")
@RequestMapping("/v1/user/")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    /**
     * 1.注册功能
     */
    @PostMapping("reg")
    @Operation(summary = "注册功能")
    @ApiOperationSupport(order = 10)
    public JsonResult reg(@Validated @RequestBody UserRegParam userRegParam){
        log.debug("userRegParam = " + userRegParam);
        /*
            1.接收客户端传递的参数[已完成];
            2.校验数据的合法性[暂时不校验,后续使用验证框架validation实现校验];
            3.校验用户名是否被占用[SELECT接口];
              SELECT COUNT(*) FROM user WHERE username=?
              3.1 被占用:返回用户名被占用;
              3.2 未被占用:将用户信息插入数据表[INSERT接口], 返回注册成功;
         */
        String username = userRegParam.getUsername();
        int count = userMapper.selectByUsername(username);
        if (count > 0){ //被占用
            return new JsonResult(StatusCode.USERNAME_EXISTS);
        }
        // 未被占用
        User user = new User();
        BeanUtils.copyProperties(userRegParam, user);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        log.debug("user = " + user);
        // 调用接口方法
        userMapper.insertUser(user);
        return JsonResult.ok();
    }

    /**
     * 2.登录功能
     */
    @PostMapping("login")
    @Operation(summary = "登录功能")
    @ApiOperationSupport(order = 20)
    public JsonResult login(@RequestBody UserLoginParam userLoginParam, HttpSession session){
        log.debug("userLoginParam = " + userLoginParam);
        UserVO userVO = userMapper.selectByUsernameAndPassword(userLoginParam);
        if (userVO == null){
            return new JsonResult(StatusCode.USERNAME_PASSWORD_ERROR);
        }
        //将用户信息保存到session中
        session.setAttribute("user", userVO);
        //登录成功
        return JsonResult.ok(userVO);
    }

    /**
     * 3.获取当前用户功能
     */
    @GetMapping("currentUser")
    @Operation(summary = "获取当前用户功能")
    @ApiOperationSupport(order = 30)
    public JsonResult currentUser(HttpSession session){
        //服务器内存中获取会话标识
        UserVO userVO = (UserVO) session.getAttribute("user");

        //后端情况1:{"state":2000, "message":"操作成功", "data":userVO}
        //后端情况2:{"state":2000, "message":"操作成功", "data":null}
        //前端工程师判断[JavaScript语言]: if(data != null){...}else{...}
        return JsonResult.ok(userVO);
    }

    /**
     * 4.退出登录
     * 测试思路:
     *   1.登录成功;
     *   2.获取当前用户[可以获取到];
     *   3.退出登录;
     *   4.获取当前用户[获取不到];
     */
    @GetMapping("logout")
    @Operation(summary = "退出登录")
    @ApiOperationSupport(order = 40)
    public void logout(HttpSession session){
        //在服务器内存中删除会话标识
        session.removeAttribute("user");
    }

    /**
     * 示例: HttpServletRequest
     */
    @PostMapping("reg666")
    public JsonResult reg666(HttpServletRequest request){
        //获取客户端传递的参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String email = request.getParameter("email");
        String phone = request.getParameter("phone");
        //获取客户端IP
        String addr = request.getRemoteAddr();
        //获取会话
        HttpSession session = request.getSession();
        //请求方法
        String method = request.getMethod();

        return JsonResult.ok();
    }
}











